08. Collections - Stack and Queue

035ND C01 L01 A09 QUEUE AND STACK

Here we are going to introduce two interesting data structures, stack and queue.

Stack follows last-in-first-out (LIFO) principle. That’s when you add an element, you add that to the top, when you remove an element, you also remove it from the top. Similar to the two methods what we just learned from LinkedList, offerFirst, and removeFirst, which are equivalent to Stack’s push and pop.

On the other hand, Queue follows first-in-first-out (FIFO) principle. An excellent example of a queue is a line of customers waiting to buy ice cream. The first customer will get the ice cream first, and when they get the ice cream, they will leave the queue. Similar to two methods we learned from LinkedList, offerLast, and removeFirst, which are equivalent to Queue’s enqueue, and dequeue.

Resources:

Queue: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html

Stack: https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html

Stack and Queue

Which collection follows FIFO principle

SOLUTION: Queue

Queue and Stack Exercise

Task Description:

Complete the calculator coding question.

Give a string, and implement a calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, * operators.

Example1: input “1+2”, output:3

Example2: input “1+2*5”, output:11

public static int calculate(String s) {
}
Task List:

035ND C01 L01 A10 QUEUE AND STACK WALKTHROUGH